package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.BbqugeParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class BbqugeCrawl extends BaseCrawl {
	private static HttpUtil httpBook = new HttpUtil("GBK");
	private static HttpUtil httpChapter = new HttpUtil("UTF-8");
	private static Logger logger = Logger.getLogger(BbqugeCrawl.class);

	public BbqugeCrawl() {
		tag = Book.TYPE_BBQUGE;
		httpBook.setTIMEOUT(200*1000) ;
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param webId
	 */
	public void crawlBook(Book book) {
		//http://www.bbquge.com/book/55.html
		String describUrl = "http://www.bbquge.com/book/" + book.webId + ".html";
		String page = "";
		try {
			logger.info("读取简介:" + describUrl); // 
			Map<String, String> headMap = new HashMap<String, String>() ;
			headMap.put("Referer", "http://www.bbquge.com") ;
			headMap.put("Connection", "keep-alive") ;
//			headMap.put("Host", "www.bbquge.com") ;
//			headMap.put("If-None-Match", (System.currentTimeMillis()-100*24*60*60*1000)+"|") ;
//			headMap.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0") ;
			page = httpBook.getPage(describUrl, headMap);
			if (ClassUtil.isEmpty(book.describ)) {
				BbqugeParser.parseDescrib(page, book);
			}
			//http://www.bbquge.com/files/article/html/0/55/index.html
			String categoryUrl =   book.bookUrl  ;
			logger.info("读取目录:" + categoryUrl);
			page = httpBook.getPage(categoryUrl);
//			logger.debug(page);
			List<Chapter> list = BbqugeParser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		// http://www.bbquge.com/files/article/html/0/55/index.html
		String urlStr =  chapter.chapterUrl;
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook.getPage(urlStr);
			} catch (Exception e) {
				// e.printStackTrace();
				logger.error(e);
			}
		}
		page = filter(page);
		BbqugeParser.parseChapter(page, chapter);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		page = page.replaceAll("<img.+?>", "");
		page = page.replaceAll("笔趣阁", ""); 
		page = page.replaceAll("&.{2,7};", "");
		 page = page.replaceAll("www.bbquge.me", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.bookId = 63;
		book.webId = "55"; //
		BbqugeCrawl crawl = new BbqugeCrawl();
		crawl.crawlBook(book);
//		System.out.println(System.currentTimeMillis());
	}

}
